package com.xci.edu.module.foreground.pc.train.controller;

import com.github.lvyanyang.core.BaseWebController;
import com.github.lvyanyang.service.CoreService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/trainLoginCaptcha")
public class TrainLoginCaptchaController extends BaseWebController {

    /** 验证码 */
    @Resource
    private CoreService coreService;

    @ResponseBody
    @GetMapping(value = "/captcha", produces = {"image/jpeg"})
    public void captcha() {
        var width = 160;        //定义验证码宽度
        var height = 48;        //定义验证码高度
        var type = "";   //定义验证码类型：[math-算数运算, gif-动画字母数字, chinese-汉字, 空-普通字母数字]
        //1. 生成验证码对象
        var model = coreService.buildCaptcha(width, height, type);
    /*
        model.text          验证码显示文本
        model.value         验证码应该输入值
        model.captcha       验证码对象
        model.contentType   HTTP内容类型
    */

        //2. 把生成的验证码答案保存起来，可以放Session或者Cache，验证的时候能取出来就行
        getSession().setAttribute("trainLoginCode", model.getValue());
        System.out.println("-----trainLoginCaptcha---"+model.getValue());
        //3. 把验证码对象写入Response中
        coreService.writeCaptchaToResponse(getResponse(), model);
    }
}
